﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace Vehicle.Web.Pages.Task
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Pilot_Edit.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-09-02   Created by 杨光远 V1.00.0000
    /// 功能说明：添加任务信息页面类
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Task_Add : Component.PageBase
    {
        #region ---基本变量实例化---

        Vehicle.BLL.Task b_Task = new Vehicle.BLL.Task();//定义逻辑层变量
        BLL.Log b_Log = new Vehicle.BLL.Log();
        Vehicle.Model.TaskInfo m_TaskInfo = new Vehicle.Model.TaskInfo();//定义实体层
        Vehicle.BLL.Vehicles b_Vehicles = new Vehicle.BLL.Vehicles();//定义逻辑层变量
        Vehicle.BLL.Motorcade b_Motorcade = new Vehicle.BLL.Motorcade();//定义逻辑层变量
        public string url;
        #endregion

        #region---基本操作---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CargoDataBind();
                MotorcadeDataBind();//绑定车辆信息
            }
        }

        /// <summary>
        /// 承运单数据绑定
        /// </summary>
        public void CargoDataBind()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {
                txt_C_Card.Text = b_Task.GetModel(new Model.ComponentsInfo("Cargo", "C_ID", Request.QueryString["id"].ToString(), "1", "1", "C_IsDelete", "1", "C_ID")).Rows[0]["C_Card"].ToString();//承运单编号
                url = Request.QueryString["id"].ToString();
            }
            else
            {
                Common.MessageHelper.AlertBack("您的操作有误,请重新尝试!");
            }

        }
        /// <summary>
        /// 初始绑定车队下拉数据
        /// </summary>
        public void MotorcadeDataBind()
        {
            b_Motorcade.DropDownListBind(drop_M_ID);//绑定车队
            drop_M_ID.Items.Remove(new ListItem("无", "0"));
            VehiclesDropDataBind();
            PilotDataBind();
        
        }
        /// <summary>
        /// 初始绑定驾驶员
        /// </summary>
        public void PilotDataBind()
        {
            txt_P_ID.Text = null;
            try
            {
                lab_P_ID.Visible = false;
                DataTable dt = b_Task.GetModel(new Model.ComponentsInfo("Pilot", "V_ID", drop_V_ID.SelectedValue, "1", "1", "P_IsDelete", "1", "P_ID"));
                txt_P_ID.Text = dt.Rows[0]["P_Name"].ToString();//驾驶员姓名
                hf_P_ID.Value = dt.Rows[0]["P_ID"].ToString();//驾驶员ID
            }
            catch
            {
                lab_P_ID.Visible = true;
            }

        }


        /// <summary>
        /// 根据车队下拉菜单绑定下属车辆
        /// </summary>
        public void VehiclesDropDataBind()
        {
            b_Vehicles.DropDownListBind(drop_V_ID, new Vehicle.Model.ComponentsInfo("Vehicles", "M_ID", drop_M_ID.SelectedValue, "0", "0", "V_IsDelete", "1", "V_ID"));
            PilotDataBind();
        }


        /// <summary>
        /// 提交信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            if (drop_V_ID.SelectedValue != "0")
            {
                //判断所选车辆是否已经有承运任务
                if (b_Task.GetModel(new Vehicle.Model.ComponentsInfo("Vehicles", "V_ID", drop_V_ID.SelectedValue, "1", "1", "V_IsDelete", "1", "V_ID")).Rows[0]["V_Transit"].ToString() != "1")
                {
                    //判断是否有相同的任务编号
                    if (b_Task.GetModel(new Vehicle.Model.ComponentsInfo("Task", "T_Num", txt_C_Num.Text.Trim(), "1", "1", "T_IsDelete", "1", "T_ID")).Rows.Count == 0)
                    {
                        m_TaskInfo.C_Card = txt_C_Card.Text.Trim();
                        m_TaskInfo.M_ID = Common.TextHelper.StringToInt(drop_M_ID.SelectedValue);//承运车队ID
                        m_TaskInfo.V_ID = Common.TextHelper.StringToInt(drop_V_ID.SelectedValue);//承运车辆ID
                        m_TaskInfo.P_ID = Common.TextHelper.StringToInt(hf_P_ID.Value);//驾驶员ID
                        m_TaskInfo.T_Time = DateTime.Now;//添加时间默认当前时间
                        m_TaskInfo.T_Note = Common.TextHelper.CheckStr(txt_V_Note.Text.Trim());//任务备注
                        m_TaskInfo.T_StarTime = Common.TextHelper.StringToDateTime(Gmdp_T_StarTime.DateString);//发车时间
                        m_TaskInfo.T_EndTime = Common.TextHelper.StringToDateTime(Gmdp_T_EndTime.DateString);//结束时间
                        m_TaskInfo.T_IsDelete = 0;//是否删除 默认0不删除
                        m_TaskInfo.T_Type = 1;//任务状态0已取消 1执行中 2 已完成
                        m_TaskInfo.T_LastTime = DateTime.Now;//最后更新时间
                        m_TaskInfo.T_Num = txt_C_Num.Text.Trim();//任务编号
                        try
                        {
                            b_Task.Add(m_TaskInfo);
                            b_Task.AllUpdate(new Model.ComponentsInfo("Pilot", "P_Transit", "1", "P_ID", hf_P_ID.Value));//更新驾驶员
                            b_Task.AllUpdate(new Model.ComponentsInfo("Vehicles", "V_Transit", "1", "V_ID", drop_V_ID.SelectedValue));//更新车辆
                            b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功添加任务编号为" + txt_C_Num.Text.Trim() + "的任务信息"));//添加日志
                            Common.MessageHelper.AlertRedirect("任务添加成功!", "../Cargo/Main.aspx");
                        }
                        catch
                        {
                            Common.MessageHelper.Alert("操作失败,请重新填写!");
                        }
                    }
                    else
                    {
                        Common.MessageHelper.Alert("任务编号有重复,请更换!");
                    }


                }
                else
                {
                    Common.MessageHelper.Alert("您所选的车辆已经有承运任务,请更换!");
                }
            }
            else
            {
                Common.MessageHelper.Alert("您没有选定车辆!");
            }

        }

        /// <summary>
        /// 车队下拉
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drop_M_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            VehiclesDropDataBind();
            
        }
        /// <summary>
        /// 车辆下拉
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drop_P_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            PilotDataBind();
        }
        #endregion
    }
}
